package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.service.facade.RateSchemeCfgService;
import com.irdstudio.efp.console.service.vo.RateSchemeCfgVO;
import com.irdstudio.efp.console.service.vo.RateSchemeDetailVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class RateSchemeCfgController extends AbstractController  {
	
	@Autowired
	@Qualifier("rateSchemeCfgService")
	private RateSchemeCfgService rateSchemeCfgService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 */
	@RequestMapping(value="/rate/scheme/cfgs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<RateSchemeCfgVO>> queryRateSchemeCfgAll(
			@RequestBody RateSchemeCfgVO vo) {
		// 根据用户角色获取用户权限方案
		List<RateSchemeCfgVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = rateSchemeCfgService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = rateSchemeCfgService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = rateSchemeCfgService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = rateSchemeCfgService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	/**
	 * 有效列表数据查询
	 */
	@RequestMapping(value="/rate/scheme/cfgs/valid", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<RateSchemeCfgVO>> queryRateSchemeCfgValid(@RequestBody RateSchemeCfgVO vo) {
		setUserInfoToVO(vo);
        List<RateSchemeCfgVO> outputVo = rateSchemeCfgService.queryAllValidByPage(vo);
		return getResponseData(outputVo);

	}
	/**
	 * 根据主键查询详情
	 */
	@RequestMapping(value="/rate/scheme/cfg/{schemeNo}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<RateSchemeCfgVO> queryByPk(@PathVariable("schemeNo") String schemeNo) {		
		RateSchemeCfgVO inVo = new RateSchemeCfgVO();
				inVo.setSchemeNo(schemeNo);
		RateSchemeCfgVO outputVo = rateSchemeCfgService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 */
	@RequestMapping(value="/rate/scheme/cfg", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody RateSchemeCfgVO inRateSchemeCfgVo) {
		setUserInfoToVO(inRateSchemeCfgVo);
		int outputVo = rateSchemeCfgService.deleteByPk(inRateSchemeCfgVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 */
	@RequestMapping(value="/rate/scheme/cfg", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody RateSchemeCfgVO inRateSchemeCfgVo) {
        setUserInfoToVO(inRateSchemeCfgVo);
        List<RateSchemeDetailVO> rateSchemeDetailVOLIst =inRateSchemeCfgVo.getRateSchemeDetailVOLIst();
        if (rateSchemeDetailVOLIst!=null&&rateSchemeDetailVOLIst.size()!=0)
            for(int i=0;i<rateSchemeDetailVOLIst.size();i++){
                rateSchemeDetailVOLIst.set(i,setUserInfoToVO(rateSchemeDetailVOLIst.get(i)));
            }
        int outputVo = rateSchemeCfgService.updateByPk(inRateSchemeCfgVo);
        return getResponseData(outputVo);
		
	}

	/**
	 * 根据主键更新状态
	 */
	@RequestMapping(value="/rate/scheme/cfg/status", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> setStatusByPk(@RequestBody RateSchemeCfgVO inRateSchemeCfgVo) {
		setUserInfoToVO(inRateSchemeCfgVo);
		List<RateSchemeDetailVO> rateSchemeDetailVOLIst =inRateSchemeCfgVo.getRateSchemeDetailVOLIst();
		if (rateSchemeDetailVOLIst!=null&&rateSchemeDetailVOLIst.size()!=0)
			for(int i=0;i<rateSchemeDetailVOLIst.size();i++){
				rateSchemeDetailVOLIst.set(i,setUserInfoToVO(rateSchemeDetailVOLIst.get(i)));
			}
		int outputVo = rateSchemeCfgService.setStatusByPk(inRateSchemeCfgVo);
		return getResponseData(outputVo);

	}
	
	/**
	 * 新增数据
	 */
	@RequestMapping(value="/rate/scheme/cfg", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertRateSchemeCfg(@RequestBody RateSchemeCfgVO inRateSchemeCfgVo) {
        setUserInfoToVO(inRateSchemeCfgVo);
        List<RateSchemeDetailVO> rateSchemeDetailVOLIst =inRateSchemeCfgVo.getRateSchemeDetailVOLIst();
        if (rateSchemeDetailVOLIst!=null&&rateSchemeDetailVOLIst.size()!=0)
            for(int i=0;i<rateSchemeDetailVOLIst.size();i++){
                rateSchemeDetailVOLIst.set(i,setUserInfoToVO(rateSchemeDetailVOLIst.get(i)));
            }
		int outputVo = rateSchemeCfgService.insertRateSchemeCfg(inRateSchemeCfgVo);
		return getResponseData(outputVo);
		
	}
}
